package com.example.learningspringboot.email.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
@Slf4j
public class EmailConfig {

    @Bean
    public JavaMailSender javaMailSender() {
        // 创建一个基础的 JavaMailSender
        // 具体的服务器配置可以在发送时动态设置
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        // QQ邮箱配置
        mailSender.setHost("smtp.qq.com");
        mailSender.setPort(587);
        mailSender.setUsername("870398662@qq.com");    // 替换为你的QQ邮箱
        mailSender.setPassword("fhwxcohmwkcnbcbi");       // 替换为你的SMTP授权码

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.ssl.trust", "smtp.qq.com");
        props.put("mail.smtp.connectiontimeout", "10000");
        props.put("mail.smtp.timeout", "10000");
        props.put("mail.smtp.writetimeout", "10000");
        props.put("mail.debug", "false");

        mailSender.setJavaMailProperties(props);

        log.info("✅ JavaMailSender 配置完成");
        log.info("主机: {}", mailSender.getHost());
        log.info("端口: {}", mailSender.getPort());
        log.info("用户名: {}", mailSender.getUsername());

        return mailSender;

    }
}
